#------------------------------------------------------------------------------
# Project:     Multi-Programmer Game Demo
# Name:        Ball.py
# Purpose:     Bouncing Ball Object
# 
# Authors:     Chris Spooner, Chris La-roo
# Main Author: Chris Spooner
#
# Created:     11/30/2012
# Copyright:   (c) Chris Spooner 2012
# Licence:     New BSD
#------------------------------------------------------------------------------
import pygame, math, sys, random
from robot import Robot
class Dinosaur(pygame.sprite.Sprite):
    def __init__(self, speed, position, screenSize):
        pygame.sprite.Sprite.__init__(self, self.containers)
        self.images = []
        self.images += [pygame.image.load("rsc\dino\coelophysis\coelophysis1.png"), pygame.image.load("rsc\dino\coelophysis\coelophysis.png"), pygame.image.load("rsc\dino\coelophysis\coelophysis2.png")]
        self.wait = 0
        self.waitMax = 5
        self.frame = 1
        self.maxFrame = len(self.images)-1
        self.image = self.images[self.frame]
        self.rect = self.image.get_rect()
        self.radius = self.rect.width/2.5
        self.chaseRadius = 300
        self.speed = speed
        self.maxspeed = 4
        self.realx = position[0]
        self.realy = position[1]
        self.xOffset = 0
        self.yOffset = 0
        self.scrollThreshold = 100#250
        self.scrollingx = False
        self.scrolling = False
        self.scrollingy = False
        self.damage = 3
        self.maxDamage = 5
        self.maxHealth = 50
        self.health = 50
        self.place(position)
        self.living = True
        self.didHit = False
        self.screenSize = self.screenWidth, self.screenHeight = screenSize
        self.pos = position
        #if pygame.mixer:
        #    self.bounceSound = pygame.mixer.Sound("bounce.wav")
        
    def chase(self):
        #print self.distToPoint([self.xOffset, self.yOffset])
        if self.distToPoint([self.xOffset, self.yOffset]) < self.chaseRadius:
            pX = self.xOffset
            pY = self.yOffset
            zX = self.rect.center[0]
            zY = self.rect.center[1]
           
            if pX > zX:
                self.speed[0] = self.maxspeed
            elif pX < zX:
                self.speed[0] = -self.maxspeed
            else:
                self.speed[0] = 0
       
            if pY > zY:
                self.speed[1] = self.maxspeed
            elif pY < zY:
                self.speed[1] = -self.maxspeed
            else:
                self.speed[1] = 0


    
    def place(self, position):
        self.rect.center = position
        
    def animate(self):
        if self.wait >= self.waitMax:
            self.wait = 0
            if self.frame < self.maxFrame:
                self.frame += 1
            else:
                self.frame = 0
        else:
            self.wait += 1
            
        self.image = self.images[self.frame]
    
    def distToPoint(self, pt):
        x1 = self.rect.center[0]
        x2 = pt[0]
        y1 = self.rect.center[1]
        y2 = pt[1]
        return math.sqrt(((x2-x1)**2)+((y2-y1)**2))
        
    def  __str__(self):
        return "I am a coelophysis! " + str(self.rect.center) + str(self.speed) +                str(self.living),
        pass 
     
    def move(self, player):
        if player != None:
            self.chase()
        self.rect = self.rect.move(self.speed)
        self.realx += self.speed[0]
        self.realy += self.speed[1]
        self.chase()
        self.place([self.realx-self.xOffset, self.realy])
        #print "I've moved", self.speed
    
    def wallCollide(self):
        if (self.rect.left < 0 
            or self.rect.right > self.screenWidth):
            self.speed[0] = self.speed[0]*-1
        if (self.rect.top < 0 
            or self.rect.bottom >self.screenHeight):
			self.speed[1] = self.speed[1]*-1
            
    def robotCollide(self, other):
        if (self != other 
            and not self.didHit 
            and not other.didHit):
                if (self.distToPoint(other.rect.center) 
                    < self.radius + other.radius):
                        self.didHit = True
                        other.didHit = True
                        self.speed[0] = self.speed[0] * -1
                        self.speed[1] = self.speed[1] * -1
                        self.hurt
                        
    def dinosaurCollide(self, other):
        if (self != other 
            and not self.didHit                               
            and not other.didHit):
                if (self.distToPoint(other.rect.center)
                    < self.radius + other.radius):
                        self.didHit = True
                        other.didHit = True
                        self.speed[0] = self.speed[0] * -1
                        self.speed[1] = self.speed[1] * -1
                        other.speed[0] = other.speed[0] * -1
                        other.speed[1] = other.speed[1] * -1
                        return True
        return False
                        
                        
    def bulletCollide(self, other):
        if (self != other 
            and not self.didHit                               
            and not other.didHit):
                if (self.distToPoint(other.rect.center)
                    < self.radius + other.radius):
                        self.didHit = True
                        other.didHit = True
                        self.kill()
                        print "collide dead"
                        
    def shotgunCollide(self, other):
        if (self != other 
            and not self.didHit                               
            and not other.didHit):
                if (self.distToPoint(other.rect.center)
                    < self.radius + other.radius):
                        self.didHit = True
                        other.didHit = True
                        self.kill()
                        print "collide dead"
                        
    def hurt(self, amount):
        self.life -= amount
        if self.life <= 0:
            self.kill
            
    
    def update(*args):
        self = args[0]
        self.xOffset = args[1]
        self.yOffset = args[2]
        self.scrollingx = args[3]
        self.scrollingy = args[4]
        self.didHit = False
        self.animate()
        self.move(self)
        self.wallCollide()
        
